﻿using DataType.CustomEnumType;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataType
{
    public class Weapon
    {
        public Weapon(WeaponType weaponName, BulletType bulletType, int rangeVehicleIn = -1, Dictionary<int, int[]> attackLevelsVehicleIn = null, int rangeInfantryIn = -1, Dictionary<int, int[]> attackLevelsInfantryIn = null)
        {
            name = weaponName;
            bullet = bulletType;
            if (rangeVehicleIn <= 0 && rangeInfantryIn <= 0)
            {
                throw new Exception($"关于{weaponName}的武器表数据有误，必要对车辆或步兵算子有损伤");
            }
            if (rangeVehicleIn <= 0)
            {
                canShootVehicle = false;
            }
            else
            {
                //if (attackLevelsVehicleIn.Length != rangeVehicleIn + 1)
                //{
                //    throw new Exception($"关于{weaponName}的武器表数据有误，射程与攻击等级表不匹配");
                //}
                int blood_count = attackLevelsVehicleIn.Count;
                for (int i = 1; i < blood_count + 1; i++)
                {
                    //表 是 从0 开始，所以射击范围要加1
                    if (rangeVehicleIn + 1 != attackLevelsVehicleIn[i].Length)
                    {
                        throw new Exception($"关于{weaponName}的武器表数据有误，射程与攻击等级表不匹配");
                    }
                }
                canShootVehicle = true;
                rangeVehicle = rangeVehicleIn;
                attackLevelsVehicle = attackLevelsVehicleIn;
            }
            if (rangeInfantryIn <= 0)
            {
                canShootInfantry = false;
            }
            else
            {
                int blood_count = attackLevelsInfantryIn.Count;
                for (int i = 1; i < blood_count + 1; i++)
                {
                    //表 是 从0 开始，所以射击范围要加1
                    if (rangeInfantryIn + 1 != attackLevelsInfantryIn[i].Length)
                    {
                        throw new Exception($"关于{weaponName}的武器表数据有误，射程与攻击等级表不匹配");
                    }
                }
                canShootInfantry = true;
                rangeInfantry = rangeInfantryIn;
                attackLevelsInfantry = attackLevelsInfantryIn;
            }
        }
        //步兵轻武器
        //public Weapon(WeaponType weaponName, BulletType bulletType, int rangeVehicleIn = -1, Dictionary<int, int[]> attackLevelsVehicleIn = null, int rangeInfantryIn = -1, Dictionary<int, int[]> attackLevelsInfantryIn = null)
        //{
        //    if (weaponName != WeaponType.InfantryLightWeapon)
        //    {
        //        throw new Exception($"关于{weaponName}的武器表数据有误，对车辆的攻击等级表赋值给了字典类型的变量");
        //    }
        //    name = weaponName;
        //    bullet = bulletType;
        //    if (rangeVehicleIn <= 0 && rangeInfantryIn <= 0)
        //    {
        //        throw new Exception($"关于{weaponName}的武器表数据有误，必要对车辆或步兵算子有损伤");
        //    }
        //    if (rangeVehicleIn <= 0)
        //    {
        //        canShootVehicle = false;
        //    }
        //    else
        //    {
        //        int blood_count = attackLevelsInfantryIn.Count;
        //        for (int i = 1; i < blood_count + 1; i++)
        //        {
        //            //表 是 从0 开始，所以射击范围要加1
        //            if (attackLevelsVehicleIn[i].Length != rangeVehicleIn + 1)
        //            {
        //                throw new Exception($"关于{weaponName}的武器表数据有误，射程与攻击等级表不匹配");
        //            }
        //        }
        //        canShootVehicle = true;
        //        rangeVehicle = rangeVehicleIn;
        //        attackLevelsVehicleOfInfantryLightWeapon = attackLevelsVehicleIn;
        //    }
        //    if (rangeInfantryIn <= 0)
        //    {
        //        canShootInfantry = false;
        //    }
        //    else
        //    {
        //        int blood_count = attackLevelsInfantryIn.Count;
        //        for (int i = 1; i < blood_count + 1; i++)
        //        {
        //            //表 是 从0 开始，所以射击范围要加1
        //            if (rangeInfantryIn + 1 != attackLevelsInfantryIn[i].Length)
        //            {
        //                throw new Exception($"关于{weaponName}的武器表数据有误，射程与攻击等级表不匹配");
        //            }
        //        }
        //        canShootInfantry = true;
        //        rangeInfantry = rangeInfantryIn;
        //        attackLevelsInfantry = attackLevelsInfantryIn;
        //    }
        //}

        private WeaponType name;
        private BulletType bullet;
        private bool canShootVehicle;
        private int rangeVehicle;
        //private int[] attackLevelsVehicle;
        private Dictionary<int, int[]> attackLevelsVehicle;
        private bool canShootInfantry;
        private int rangeInfantry;
        private Dictionary<int, int[]> attackLevelsInfantry;
        //步兵轻武器对车辆的攻击等级表
        //private Dictionary<int, int[]> attackLevelsVehicleOfInfantryLightWeapon;

        public WeaponType Name => name;

        public BulletType Bullet => bullet;

        public int GetRange(OprType targetType, int opr_blood)
        {
            if (targetType == OprType.Vehicle)
            {
                if (canShootVehicle)
                {
                    return rangeVehicle;
                }
                else
                {
                    Trace.TraceWarning($"武器{name}不能以{targetType}类型算子为设计目标");
                    return -1;
                }
            }
            else if (targetType == OprType.Infantry)
            {
                if (canShootInfantry)
                {
                    return rangeInfantry;
                }
                else
                {
                    Trace.TraceWarning($"武器{name}不能以{targetType}类型算子为设计目标");
                    return -1;
                }
            }
            else
            {
                Trace.TraceWarning($"武器{name}不能以{targetType}类型算子为设计目标");
                return -1;
            }
        }

        public int GetAttackLevel(OprType targetType, int distance, int opr_blood)
        {
            if (targetType == OprType.Vehicle)
            {
                if (canShootVehicle)
                {
                    if (distance >= 0 && distance <= rangeVehicle)
                    {
                        if(name == WeaponType.InfantryLightWeapon)
                        {
                            return attackLevelsVehicle[opr_blood][distance];
                        }
                        else return attackLevelsVehicle[1][distance];
                    }
                    else
                    {
                        //Trace.TraceWarning($"输入的距离参数不在武器{name}射程内");
                        return -1;
                    }
                }
                else
                {
                    //Trace.TraceWarning($"武器{name}不能以{targetType}类型算子为设计目标");
                    return -1;
                }
            }
            else if (targetType == OprType.Infantry)
            {
                if (canShootInfantry)
                {
                    if (distance >= 0 && distance <= rangeInfantry)
                    {
                        return attackLevelsInfantry[opr_blood][distance];
                    }
                    else
                    {
                        //Trace.TraceWarning($"输入的距离参数不在武器{name}射程内");
                        return -1;
                    }
                }
                else
                {
                    //Trace.TraceWarning($"武器{name}不能以{targetType}类型算子为设计目标");
                    return -1;
                }
            }
            else
            {
                //Trace.TraceWarning($"武器{name}不能以{targetType}类型算子为设计目标");
                return -1;
            }
        }
    }
}
